<template>
	<xn-form-container
		:title="'编辑使用情况'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<license-detail v-model:form-data="formData" />
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="horizontal">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="发放使用状态：" name="state">
						<a-select v-model:value="formData.state" placeholder="请选择发放状态" :options="stateOptions" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="licenseApplicationForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import licenseApplicationApi from '@/api/biz/licenseApplicationApi'
	import bizGroupApi from '@/api/biz/bizGroupApi'
	import customerInfoApi from '@/api/biz/customerInfoApi'
	import softwareVersionApi from '@/api/biz/softwareVersionApi'
	import LicenseDetail from "@/views/biz/licenseapplication/LicenseDetail.vue";
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const buildStateOptions = ref([])
	const approvalStatusOptions = ref([])
	const stateOptions = ref([])
	const typeOptions = ref([])
	const versionOptions = ref([])
	const applicants = ref([])
	const customs = ref([])
	const module5 = 'security,commonapp,pm,spa,mt,fp,scm,scmt,requirement'
	const module8 = 'sys,biz,auth,dev,pm,sp,sec,mt,cm,flow,netdisk,rdm,train'
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		buildStateOptions.value = tool.dictList('BUILD_STATE')
		approvalStatusOptions.value = tool.dictList('APPROVAL_STATUS')
		stateOptions.value = tool.dictList('LICENSE_STATUS')
		typeOptions.value = tool.dictList('LICENSE_TYPE')
		bizGroupApi.bizGroupOwnUserEntity({ id: '1930923922069581826' }).then((res) => {
			applicants.value = res
		})
		customerInfoApi.customerInfoList().then((res) => {
			customs.value = res
		})
		softwareVersionApi.softwareVersionList().then((res) => {
			versionOptions.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		applicants.value = []
		customs.value = []
		open.value = false
	}
	const onVersionChange = (value) => {
		if (value) {
			softwareVersionApi.softwareVersionDetail({ id: value }).then((res) => {
				if ('5.0' === res.builderType) {
					formData.value.module = module5
				} else if ('8.0' === res.builderType) {
					formData.value.module = module8
				}
			})
		}
	}
	// 默认要校验的
	const formRules = {
		applicantId: [required('请输入申请人')],
		customerId: [required('请输入客户')],
		maxUser: [required('请输入最大数量')],
		state: [required('请输入发放状态')],
		type: [required('请输入授权类型')],
		version: [required('请输入版本')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				licenseApplicationApi
					.licenseApplicationSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
